//index.js
//获取应用实例
const app = getApp()
const Api = require("../../utils/api.js").API
const Store = require("../../utils/store.js").store
const codeZ = require("../../utils/util.js")
Page({
  data: { 
    goodsName: '',
    goodsSynopsis: '',
    catenation: '',
    files: [],
    spus: [],
    getCatenationTypes: [
        {label: "淘宝", value: "109401"},
        {label: "京东", value: "109402"}
    ],
    show: false,
    showCatenationType: false,
    spu: {},
    catenationType: {},
    baseinfo: {}
  },
  onShow:function(){
    let _this = this
    const eventChannel = this.getOpenerEventChannel()
    // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
    eventChannel.on('acceptDataFromGoodsLookDetailPage', function(data) {
      console.log(data.item)
      _this.setData({
        item: data.item,
        catenation: data.item.catenation,
        goodsName: data.item.goodsName,
        goodsSynopsis: data.item.goodsSynopsis,
        files: data.item.file.split(','),
        spu: {spuNo: data.item.spuNo}
      })
    })
    let baseinfo =  wx.getStorageSync('baseInfo');
    this.setData({
      baseinfo: baseinfo
    })
    this.getSpu()
  }, 
  save: function() {
      let _this = this
      let data = {
        catenation: this.data.catenation,
        catenationType: this.data.catenationType.value,
        file: this.data.files.join(','),
        goodsName: this.data.goodsName,
        goodsSynopsis: this.data.goodsSynopsis,
        spuNo: this.data.spu.spuNo
      }
      console.log(data)
      Api.lookGoodsSaveApi(data).then(res => {
          console.log(res)
          if (res.code === 0) {
            wx.showToast({
                title: '添加成功',
                icon: 'success'
            })
            wx.switchTab({
              url: 'pages/GoodsList/index',
            })
          }
      })
  },
  confirm: function(event) {
    const e = event.detail;
    this.setData({
        show: false,
        spu: e.value
    })
  },
  confirmType: function(event) {
    const e = event.detail;
    this.setData({
        showCatenationType: false,
        catenationType: e.value
    })
  },
  getCatenationType: function() {
      // 1094
  },
  show3Popup: function() {
    this.setData({ showCatenationType: true });
  },
  showPopup: function() {
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ show: false });
  },
  getSpu: function() {
      let _this = this
    Api.getSpusApi().then(res => {
        if (res.code == 0) {
            _this.setData({
                spus: res.data
            })
        }
    })
  },
  uploadPic: function() {
    var _this = this;
        wx.chooseImage({
            count: 1, // 默认9
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'], //
            success: function(res) {
                var tempFilePaths = res.tempFilePaths;
                 let a = `list.fileInfoReqs[0].attUrl`;
                tempFilePaths.forEach((item,i)=>{
                    Api.ZhuFuUploadApi(item,{}).then(resa=>{ 
                         var resUrl = JSON.parse(resa)
                         if (resUrl.code === 0) {
                            let files = _this.data.files
                            files.push(resUrl.url)
                            _this.setData({
                                files: files
                            })
                            console.log(files)
                         }
                    });
                    
                })              
            }
        })
    },
})
